package com.topscomm.appv2;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

import com.topscomm.pub.util.ConvertUtil;

import lombok.Data;

/**
 * @description:树形节点模型
 * @author: hudeyong
 * @date: 2022年12月28日
 * @modify:
 */
@Data
public class AppTreeModel implements Serializable {
    private static final long serialVersionUID = 4013193970046502756L;
    protected String id;
    protected String pid;
    protected String code;
    protected String name;
    protected String mobileCode;
    protected String mobileName;
    protected String type;
    protected String icon;
    protected String sortcode;
    protected String routerpath;
    protected List<AppTreeModel> value;

    public AppTreeModel() {
    }

    /**
     * @description:构建树模型
     * @param listModule
     * @return
     * @author: hudeyong
     * @date: 2022年12月28日
     * @modify:
     */
    public List<AppTreeModel> buildTree(List<Map<String, Object>> treeNodes, boolean isAddRoot, String rootName) {
        // 如果支持根节点，则增加默认根节点
        if (isAddRoot == true) {
            Map<String, Object> rootMap = new HashMap<String, Object>();
            rootMap.put("id", "0");
            rootMap.put("pid", "-1");
            rootMap.put("name", rootName);
            treeNodes.add(rootMap);
        }
        // 将参数Map转换为无上下级的树节点
        List<AppTreeModel> tempTreeModels = new ArrayList<AppTreeModel>();
        for (Map<String, Object> treeNode : treeNodes) {
            AppTreeModel treeModel = new AppTreeModel();
            treeModel.copyValues(treeNode);
            tempTreeModels.add(treeModel);
        }
        // 构建上下级的树节点
        List<AppTreeModel> treeModels = new ArrayList<AppTreeModel>();
        // 构建节点
        for (AppTreeModel tempTreeModel : tempTreeModels) {
            if (tempTreeModel.getId().length() <= 1) {
                treeModels.add(tempTreeModel);
                continue;
            }
            boolean haveParent = false;
            for (AppTreeModel parentTreeModel : tempTreeModels) {
                if (ConvertUtil.convertToString(tempTreeModel.getPid())
                        .equals(ConvertUtil.convertToString(parentTreeModel.getId()))) {
                    if (parentTreeModel.getValue() == null) {
                        parentTreeModel.setValue(new ArrayList<AppTreeModel>());
                    }
                    parentTreeModel.getValue().add(tempTreeModel);
                    haveParent = true;
                    break;
                }
            }
            if (haveParent == false) {
                treeModels.add(tempTreeModel);
            }
        }
        return treeModels;
    }

    /**
     * @description:
     * @param map
     * @author: hudeyong
     * @date: 2022年12月28日
     * @modify:
     */
    public void copyValues(Map<String, Object> map) {
        this.id = ConvertUtil.convertToString(map.get("id"));
        this.pid = ConvertUtil.convertToString(map.get("parentid"));
        this.name = ConvertUtil.convertToString(map.get("name"));
        this.code = ConvertUtil.convertToString(map.get("code"));
        this.mobileCode = ConvertUtil.convertToString(map.get("mobileCode"));
        this.mobileName = ConvertUtil.convertToString(map.get("mobileName"));
        this.type = ConvertUtil.convertToString(map.get("type"));
        this.icon = ConvertUtil.convertToString(map.get("icon"));
        this.sortcode = ConvertUtil.convertToString(map.get("sortcode"));
        this.routerpath = ConvertUtil.convertToString(map.get("routerpath"));
    }
}
